//package com.md.api.common.config;
//
//import org.springframework.stereotype.Component;
//
//import javax.servlet.*;
//import javax.servlet.http.HttpServletRequest;
//import javax.servlet.http.HttpServletResponse;
//import java.io.IOException;
//
//@Component
//public class CorsFilter implements Filter {
//
//    @Override
//    public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException {
//        HttpServletResponse response = (HttpServletResponse) res;
//        HttpServletRequest request = (HttpServletRequest) req;
//
//        response.addHeader("Access-Control-Allow-Origin", request.getHeader("Referer")); // 允许所有域名跨域访问
//        response.addHeader("Access-Control-Allow-Credentials","true");
//        response.addHeader("Access-Control-Allow-Methods", "POST, GET, OPTIONS, DELETE"); // 允许的请求方法
//        response.addHeader("Access-Control-Max-Age", "3600"); // 缓存时间
//        response.addHeader("Access-Control-Allow-Headers", "x-requested-with, content-type,client,Client,token"); // 允许的请求头
//
//        // 预检请求直接返回
//        if ("OPTIONS".equalsIgnoreCase(request.getMethod())) {
//            response.setStatus(HttpServletResponse.SC_OK);
//        } else {
//            chain.doFilter(req, res);
//        }
//    }
//
//    // 其他必要的方法可以根据需要实现
//    @Override
//    public void init(FilterConfig filterConfig) {
//    }
//
//    @Override
//    public void destroy() {
//    }
//}
